<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Activity;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ActivityController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('活动列表')
            ->description('列表')
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Activity(['user']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('user.name','发布人');
            $grid->column('title','主题');
            $grid->column('image','图片')->image('',60,60);
            $grid->column('intro','简介');
            $grid->column('time','活动时间');
            $grid->column('status','状态')->using([0 => '下架', 1 => '上架'])->switch();
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Activity(['user']), function (Show $show) {
            $show->field('id');
            $show->field('user.name','发布人');
            $show->field('title','主题');
            $show->field('image')->image();
            $show->field('intro','简介');
            $show->field('time','时间');
            $show->field('status');
            $show->field('created_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Activity(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->textarea('intro');
            $form->datetime('time')->format('YYYY-MM-DD HH:mm');;
            $form->text('status');
        });
    }
}
